<template>
  <div class="box">
    <div class="container product-detail">
      <el-breadcrumb separator="/" class="breadcrumb">
        <el-breadcrumb-item>
          <NuxtLink :to="{ path: '/' }">
            {{$t('header.top.nav_list.home')}}
          </NuxtLink>
        </el-breadcrumb-item>
        <el-breadcrumb-item>{{ goodDetail.name }}</el-breadcrumb-item>
      </el-breadcrumb>
      <div class="product-box">
        <div class="picture">
          <el-carousel :autoplay="false" arrow="always" height="450px" indicator-position="outside">
            <el-carousel-item v-for="(item, index) in resources_many" :key="index">
              <template v-if="item.type === 'img'">
                <el-image class="image" fit="scale-down" :src="item.img" :preview-src-list="resources_many_img"></el-image>
              </template>
              <template v-else>
                <VueVideo :sources="item.img" :poster="poster" />
              </template>
            </el-carousel-item>
          </el-carousel>
        </div>
        <div class="parameter">
          <div class="title">{{goodDetail.name}}<span class="title-price" v-if="goodDetail.integral>0">+&nbsp;{{ goodDetail.integral }}</span>
          </div>
          <template v-if="goodDetail.is_show === 0">
            <div class="sell-out">{{$t('product.sold_out')}}~</div>
          </template>
          <template v-if="!inventoryFlag">
            <div class="sell-out">{{$t('product.sell_out')}}~</div>
          </template>
          <template v-else>
            <div v-if="goodDetail.short_description" class="description">{{goodDetail.short_description}}</div>
            <div class="price-box">
              <!-- 已选择规则-->
              <template v-if="specificationDefaultDisplay.price_show">
                <template v-if="goodDetail.price_show && specificationDefaultDisplay.price_show">
                  <div class="price" v-if="specificationDefaultDisplay.price_show.length > 1"><span
                      class="symbol">{{$t('common.unit')}}</span>{{specificationDefaultDisplay.price_show[0] | thousands}} -
                    {{specificationDefaultDisplay.price_show[1] | thousands}}원</div>
                  <div class="price" v-else-if="specificationDefaultDisplay.price_show.length === 1"><span
                      class="symbol">{{$t('common.unit')}}</span>{{specificationDefaultDisplay.price_show[0] | thousands}}원</div>

                </template>
              </template>
              <!-- 未选择规则-->
              <template v-else>
                <template v-if="goodDetail.price_show">
                  <!-- <div class="price" v-if="goodDetail.price_show.length > 1"><span class="symbol">{{$t('common.unit')}}</span>{{ goodDetail.price_show[0] | thousands }} -
                    {{ goodDetail.price_show[1] | thousands }}원</div>
                  <div class="price" v-else-if="goodDetail.price_show.length === 1"><span class="symbol">{{$t('common.unit')}}</span>{{ goodDetail.price_show[0] | thousands }}원
                  </div> -->
                  <div class="price"><span class="symbol">{{$t('common.unit')}}</span>{{ (goodDetail.order_price||0) | thousands }}원
                  </div>
                </template>
                <template v-if="goodDetail.good_sku.length > 0 && goodDetail.market_price_show">
                  <div class="m-price" v-if="goodDetail.market_price_show.length > 1"><span
                      class="symbol">{{$t('common.unit')}}</span>{{ goodDetail.market_price_show[1] | thousands }}원</div>
                  <div class="m-price" v-else-if="goodDetail.market_price_show.length === 1"><span
                      class="symbol">{{$t('common.unit')}}</span>{{ goodDetail.market_price_show[0] | thousands }}원</div>
                </template>
              </template>
            </div>
            <div class="text-tag">
              <el-tag effect="dark" type="danger" size="small" class="red">무료배송</el-tag>
              <el-tag effect="dark" type="danger" size="small" class="red">더케이 직구</el-tag>
            </div>
            <el-divider></el-divider>
            <div class="sku">
              <sku ref="sku" :getList="goodDetail" @purchasePattern="purchasePattern"></sku>
            </div>
            <div class="purchase_number" v-if="goodDetail.purchase_number">{{$t('product.time_limit')}}{{ goodDetail.purchase_number }}{{$t('good_indent.piece')}}</div>
            <el-divider></el-divider>
            <div class="shipping-address">

            </div>
            <!-- bug 编译不了 -->
            <div class="operation">
              <el-button type="danger" plain @click="buy(true)" :disabled="goodDetail.state === 0"><b>{{$t('product.buy')}}</b></el-button>
              <!-- <template>
                <template v-if="goodDetail.type !== $t('product.type.keys') && goodDetail.type !== $t('product.type.download')">
                </template>
              </template> -->
              <template>
                <el-button type="info" @click="card"><b>장바구니 담기</b></el-button>
              </template>
              <template>
                <el-button type="info" :class="{'product-detail-on' : collect}" icon="el-icon-star-off" @click="toCollect"><b>{{$t('product.collect')}}</b></el-button>
              </template>
            </div>
            <!-- <span class="el-tag el-tag--warning el-tag--light" style="margin-top: 20px;">积分赠送：200</span> -->
          </template>
        </div>
      </div>
    </div>
    <el-divider></el-divider>
    <!-- 详情-->
    <div class="product-box">
      <div class="tab">
        <span :class="{on:tab === 1}" @click="cutTab(1)">{{$t('product.details')}}</span>
      </div>
      <div class="detail-box">
        <div class="container" v-loading="tabLoading">
          <div v-if="tab === 1" v-html="goodDetail.details"></div>
        </div>
      </div>
    </div>
  </div>
</template>


<script>
import { detail } from '@/api/good'
import { create as collectCreate, destroy as collectDestroy, detail as getCollectDetail } from '@/api/collect'
import sku from '@/components/Sku'
import VueVideo from '@/components/VueVideo'
import 'video.js/dist/video-js.css'
export default {
  components: {
    sku,
    VueVideo
  },
  data() {
    return {
      tab: 1,
      inventoryFlag: true, //true有货; false 无货
      tabLoading: false,
      goodDetail: {},
      specificationDefaultDisplay: {},
      resources_many: [],
      resources_many_img: [],
      collect: 0,
      poster: ''
    }
  },
  async asyncData(ctx) {
    try {
      const { params } = ctx;
      let inventoryFlag = false
      let [goodDetailData] = await Promise.all([
        detail(params.id),
      ]);
      if (goodDetailData.inventory_show > 0) {
        inventoryFlag = true
      }
      let resources_many = [];
      let resources_many_img = [];
      let poster;
      if (goodDetailData.resources_many.length > 0) {
        goodDetailData.resources_many.forEach((item, index) => {
          if (item.depict.indexOf('_video') !== -1) {
            item.type = 'video';
            resources_many.unshift(item)
          } else if (item.depict.indexOf('_poster') !== -1) {
            poster = item.img
          } else {
            item.type = 'img';
            resources_many.push(item);
            resources_many_img.push(item.img)
          }
        })
      }
      // console.log("🚀 ~  goodDetailData: ", goodDetailData)

      return {
        inventoryFlag: inventoryFlag,
        goodDetail: goodDetailData,
        resources_many: resources_many,
        resources_many_img: resources_many_img,
        poster: poster,
      }
    } catch (err) {
      ctx.$errorHandler(err)
    }
  },
  head() {
    return {
      title: this.goodDetail.name + '-' + process.env.APP_NAME,
      meta: [
        { hid: 'index', name: this.goodDetail.name + '-' + process.env.APP_NAME, content: this.goodDetail.keywords ? this.goodDetail.keywords : process.env.APP_KEYWORD },
        { hid: 'description', name: 'description', content: this.goodDetail.short_description ? this.goodDetail.short_description : process.env.APP_DESCRIPTION },
        { hid: 'keywords', name: 'keywords', content: this.goodDetail.keywords ? this.goodDetail.keywords : process.env.APP_KEYWORD }
      ]
    }
  },
  mounted() {
    if ($nuxt.$store.state.hasLogin) {
      this.getCollect()
    }
  },
  methods: {
    //选择后返回的数据
    purchasePattern(data) {
      this.specificationDefaultDisplay = data;
    },
    buy(state) {
      if (!$nuxt.$store.state.hasLogin) {
        $nuxt.$store.commit('loginCheck');
        return false
      }
      this.$refs.sku.cart(state)
    },
    card() {
      if (!$nuxt.$store.state.hasLogin) {
        $nuxt.$store.commit('loginCheck');
        return false
      }
      this.$refs.sku.cart(false)
    },
    getCollect() {
      getCollectDetail($nuxt.$route.params.id).then(response => {
        this.collect = response
      })
    },
    // 收藏
    toCollect() {
      if (!$nuxt.$store.state.hasLogin) {
        $nuxt.$store.commit('loginCheck');
        return false
      }
      if (this.collect) {
        collectDestroy(this.goodDetail.id)
      } else {
        collectCreate(this.goodDetail)
      }
      this.collect = !this.collect
    },
    // 切换栏目
    cutTab(index) {
      this.tabLoading = true;
      this.tab = index;
      setTimeout(() => {
        this.tabLoading = false
      }, 1000)
    }
  }
}

</script>


<style lang='scss' scoped>
.product-detail-on .el-icon-star-off {
  color: #fa524c;
}
.box {
  border-top-width: 1px;
  border-top-style: solid;
  border-top-color: #dcdfe6;
  background-color: #ffffff;
}
.product-box {
  .tab {
    text-align: center;
    padding: 10px 0 20px 0;
    span {
      padding: 0 20px 0 20px;
      color: #757575;
      cursor: pointer;
    }
    .on {
      color: #fa524c;
      font-size: 15px;
      font-weight: bold;
    }
  }
  .detail-box {
    background-color: #f4f4f4;
    .container {
      text-align: center;
      overflow: hidden;
      img {
        display: block;
        margin: 0 auto;
      }
    }
  }
}
.product-detail {
  .breadcrumb {
    padding: 20px 0 20px 0;
  }
  .product-box {
    display: flex;
    .picture {
      width: 460px;
      margin-right: 20px;
      .image {
        width: 100%;
      }
    }
    .parameter {
      flex: 1;
      .title {
        font-size: 24px;
        font-weight: 400;
        color: #212121;
        display: flex;
        justify-content: space-between;
        &-price {
          border: 1px solid red;
          color: red;
          // margin-left: 100px;
          padding: 0 10px;
          border-radius: 4px;
          font-weight: 100;
          font-size: 16px;
          display: flex;
          align-items: center;
        }
      }
      .description {
        color: #b0b0b0;
        margin: 0;
        padding-top: 8px;
        line-height: 1.5;
      }
      .price-box {
        display: flex;
        padding-top: 8px;
        .price {
          font-size: 28px;
          color: #fa524c;
          .symbol {
            font-size: 14px;
          }
        }
        .m-price {
          position: relative;
          top: 3px;
          font-size: 14px;
          margin-left: 5px;
          color: #b0b0b0;
          text-decoration: line-through;
        }
      }
      .operation {
        display: flex;
      }
      .purchase_number {
        font-size: 12px;
        color: #fa524c;
        margin-left: 90px;
        padding-top: 10px;
      }
      .sell-out {
        color: #999999;
        line-height: 50px;
      }
    }
  }
}
.text-tag {
  margin-top: 10px;
  .red {
    background: red;
  }
}
</style>

